package cn.signalpha.modules.resource.controller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.cache.utils.CacheUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.secure.annotation.PreAuth;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.constant.RoleConstant;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.resource.entity.Oss;
import cn.signalpha.modules.resource.service.IOssService;
import cn.signalpha.modules.resource.vo.OssVO;
import cn.signalpha.modules.resource.wrapper.OssWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static cn.signalpha.core.cache.constant.CacheConstant.RESOURCE_CACHE;

/**
 * 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_RESOURCE_NAME + "/admin/oss")
@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
@Api(value = "对象存储接口", tags = "对象存储接口")
public class OssController extends BaseController {

	private final IOssService ossService;

	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页", notes = "传入oss")
	public R<IPage<OssVO>> page(Oss oss, Query query) {
		IPage<Oss> pages = ossService.page(Condition.getPage(query), Condition.getQueryWrapper(oss));
		return R.data(OssWrapper.build().pageVO(pages));
	}

	@GetMapping("/detail")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "详情", notes = "传入oss")
	public R<OssVO> detail(Oss oss) {
		Oss detail = ossService.getOne(Condition.getQueryWrapper(oss));
		return R.data(OssWrapper.build().entityVO(detail));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "新增或修改", notes = "传入oss")
	public R submit(@Valid @RequestBody Oss oss) {
		CacheUtil.clear(RESOURCE_CACHE, false);
		return R.status(ossService.submit(oss));
	}

	@PostMapping("/enable")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "配置启用", notes = "传入id")
	public R enable(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		CacheUtil.clear(RESOURCE_CACHE, false);
		return R.status(ossService.enable(id));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 50)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear(RESOURCE_CACHE, false);
		return R.status(ossService.deleteLogic(Func.toLongList(ids)));
	}

}
